package it.win4buy.controllers
{
    import flash.events.IEventDispatcher;

    import it.win4buy.entities.PlaceHolderVO;
    import it.win4buy.events.AlertComponentEvent;
    import it.win4buy.events.GetQuestionEvent;
    import it.win4buy.events.NavigationEvent;
    import it.win4buy.events.PlayEvent;
    import it.win4buy.events.UtilityEvent;
    import it.win4buy.models.ApplicationModel;
    import it.win4buy.presentations.AlertComponentPM;
    import it.win4buy.services.GetQuestionService;
    import it.win4buy.utility.LoggerUtilty;

    import mx.resources.ResourceManager;

    import spark.skins.spark.ApplicationSkin;

    /**
     *
     *	autore:
     *  pierangelo.mancusi@gmail.com
     *
     */

    public class PlayController
    {



        //-------------------------------------------
        //
        // Injected properties
        //
        //-------------------------------------------
        [Dispatcher]
        public var dispatcher:IEventDispatcher;

        [Inject]
        public var applicationModel:ApplicationModel;

        [Inject]
        public var alertComponentPM:AlertComponentPM;

        [Inject]
        public var log:LoggerUtilty;

        [Inject]
        public var getQuestionService:GetQuestionService;

        //-------------------------------------------
        //
        // Private Properties
        //
        //-------------------------------------------



        //-------------------------------------------
        //
        // Getters / Setters
        //
        //-------------------------------------------


        //-------------------------------------------
        //
        // Constructor
        //
        //-------------------------------------------

        public function PlayController()
        {
        }


        //-------------------------------------------
        //
        // Mediator Handlers
        //
        //-------------------------------------------


        [EventHandler(event="UtilityEvent.CREATION_COMPLETE_PLAYVIEW", priority="990")]
        /**
         * Used by the View to startup all functionality.<br>
         * Fired up by the view,onthe <b>creationComplete</b> Event.
         * @param event
         *
         */
        public function startupPlayMode(event:UtilityEvent):void
        {
            applicationModel.questionPlaying=1;
            log.logToConsole(this, "getting the Question n. " + applicationModel.questionPlaying);

            // var placeHolder:PlaceHolderVO=(applicationModel.placeHolderList.getItemAt(applicationModel.registeredPlaceholder) as PlaceHolderVO);

            //fixme
            //retrieve the question 
            //getQuestionService.execute(applicationModel.userData.userId, placeHolder.tableId, applicationModel.questionPlaying.toString());


        }

        [EventHandler(event="PlayEvent.ALERT_STARTUP", priority="990")]
        public function setAlertStartup(event:PlayEvent):void
        {
            //set ALertState's properties
            log.logToConsole(this, "setting Alert properties..");
            alertComponentPM.title=ResourceManager.getInstance().getString("labels", "alertTitleLoading");
            alertComponentPM.message=ResourceManager.getInstance().getString("labels", "alertMessageStartup");

            alertComponentPM.btCancelVisible=false;
            alertComponentPM.btConfirmVisible=false;

            refreshAlert();

        }


        [EventHandler(event="GetQuestionEvent.RESULT", priority="990")]
        [EventHandler(event="GetQuestionEvent.FAULT", priority="990")]
        public function getQuestionResult(event:GetQuestionEvent):void
        {
            switch (event.type)
            {
                case GetQuestionEvent.RESULT:

                    //salva le info della domanda

                    //start countdown
                    //probabilmente un filmato flash

                    //refreshDataBinding();
                    break;

                case GetQuestionEvent.FAULT:


                    //set AlertState
                    alertComponentPM.btCancelVisible=false;
                    alertComponentPM.btConfirmVisible=true;
                    alertComponentPM.message=event.obj as String;

                    alertComponentPM.title=ResourceManager.getInstance().getString("labels", "labelError");

                    //showError 
                    dispatcher.dispatchEvent(new NavigationEvent(NavigationEvent.VIEW_PLAY_ALERT, event.obj, true));



                    break;
            }

        }

        //-------------------------------------------
        //
        // Public Methods
        //
        //-------------------------------------------


        //-------------------------------------------
        //
        // Private Methods
        //
        //-------------------------------------------
        /**
         * Force Refreshing DataBinding in the application
         *
         */
        private function refreshDataBinding():void
        {

            dispatcher.dispatchEvent(new UtilityEvent(UtilityEvent.REFRESH_BINDING, null, true));

        }

        /**
         * Used to refresh bindings
         *
         */
        private function refreshAlert():void
        {
            dispatcher.dispatchEvent(new PlayEvent(PlayEvent.ALERT_REFRESH, null, true));
        }

    }
}